package it.davial.mgwt.server;

import static it.softphone.commons.util.LogUtil.logger; 
import static it.davial.mgwt.server.Utility.ManagmentId;
import static  it.davial.mgwt.server.Sql.Area.*;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;


import it.davial.mgwt.client.Area;



public class AreaUtils {

	
	/**
	 * find all area without criteria
	 * @return List<Area>
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static List<Area> findAllArea()throws Exception{
		
		EntityManager newEm=null;
		List<Area> result=new ArrayList<Area>();
		Utility.Logs.startFunction("AreaUtils", "findAllArea", new String[]{});
		
		try{
			EntityManagerFactory emf=JpaUtility.getEntityFactory();
			if (emf==null){
				logger.warn("EntityManagerFactory is null...");
				throw new Exception();
			}
			newEm =emf.createEntityManager();
			
			logger.info(" Area(s) found:".concat(String.valueOf(result.size()))  );
			
			result =(List<Area>)newEm.createNativeQuery(SELECT_ALL,Area.class).getResultList();
	
			logger.info(" Area(s) found:".concat(String.valueOf(result.size()))  );

			
		}catch(javax.persistence.NoResultException e){
			logger.warn("AreaUtils:findAllArea",e);
		}catch(Exception e){
			logger.error("AreaUtils:findAllArea",e);
			throw e;
		}finally{
			if (newEm!=null)newEm.close();
		}
		return result;
	}
	
	/**
	 * find all area with criteria
	 * @return List<Area>
	 * @throws Exception
	 */
	public static Area findAreaById(int id)throws Exception{
		
		EntityManager newEm=null;
		Area result=null;
		Utility.Logs.startFunction("AreaUtils", "findAreaById", new String[]{String.valueOf(id)});
		
		try{
			EntityManagerFactory emf=JpaUtility.getEntityFactory();
			if (emf==null){
				logger.warn("EntityManagerFactory is null...");
				throw new Exception();
			}
			newEm =emf.createEntityManager();
			
			
			result =(Area)newEm.createNativeQuery(SELECT_BY_ID,Area.class).
										setParameter(1,id ).getSingleResult();
	
			logger.info(" Area found:".concat(String.valueOf(result))  );
			
			
		}catch(javax.persistence.NoResultException e){
			logger.warn("AreaUtils:findAreaById",e);
		}catch(Exception e){
			logger.error("AreaUtils:findAreaById",e);
			throw e;
		}finally{
			if (newEm!=null)newEm.close();
		}
		return result;
	}
	
	/**
	 * create a area
	 * @return boolean
	 * @throws Exception
	 */
	public static boolean createArea(Area area)throws Exception{
		EntityManager newEm=null;
		EntityTransaction tx =null;
		Utility.Logs.startFunction("AreaUtils", "createArea", new String[]{area.toString()});
		try{
			EntityManagerFactory emf=JpaUtility.getEntityFactory();
			if (emf==null){
				logger.warn("EntityManagerFactory is null...");
				throw new Exception();
			}
			newEm =emf.createEntityManager();
			tx = newEm.getTransaction();
		    tx.begin();
		    area.setIdArea(ManagmentId.getNextId(IdEntity.AREA));
		    area.setDataInsert(Utility.Date.nowDateDb());
		    area.setDataUpdate(Utility.Date.nowDateDb());
			newEm.persist(area);
			logger.info("area created..." );
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			logger.error("AreaUtils:createArea",e);
			throw e;
		}finally{
			if (newEm!=null)newEm.close();
		}
		return true;
	}
	
	/**
	 * merge a area
	 * @return boolean
	 * @throws Exception
	 */
	public static boolean mergeArea(Area area)throws Exception{
		EntityManager newEm=null;
		EntityTransaction tx =null;
		Utility.Logs.startFunction("AreaUtils", "merge", new String[]{area.toString()});
		try{
			EntityManagerFactory emf=JpaUtility.getEntityFactory();
			if (emf==null){
				logger.warn("EntityManagerFactory is null...");
				throw new Exception();
			}
			newEm =emf.createEntityManager();
			tx = newEm.getTransaction();
		    tx.begin();
		    area.setDataUpdate(Utility.Date.nowDateDb());
		    area.setDataInsert(Utility.Date.webFormatToDateDb(area.getDataInsert()));

			newEm.merge(area);
			logger.info("area mergeed..." );
			 tx.commit();
		}catch(Exception e){
			tx.rollback();
			logger.error("AreaUtils:mergeArea",e);
			throw e;
		}finally{
			if (newEm!=null)newEm.close();
		}
		return true;
	}
	
	/**
	 * delete a area
	 * @return boolean
	 * @throws Exception
	 */
	public static boolean deleteArea(Area area)throws Exception{
		EntityManager newEm=null;
		EntityTransaction tx =null;
		Utility.Logs.startFunction("AreaUtils", "delete", new String[]{area.toString()});
		try{
			EntityManagerFactory emf=JpaUtility.getEntityFactory();
			if (emf==null){
				logger.warn("EntityManagerFactory is null...");
				throw new Exception();
			}
			newEm =emf.createEntityManager();
			tx = newEm.getTransaction();
		    tx.begin();
			area.setState(Utility.Costants.DISABLED);
			area.setDataUpdate(Utility.Date.nowDateDb());
		    area.setDataInsert(Utility.Date.webFormatToDateDb(area.getDataInsert()));

			logger.info(String.format("area delete...[%s]",area) );
			newEm.merge(area);
			
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			logger.error("AreaUtils:deleteArea",e);
			throw e;
		}finally{
			if (newEm!=null)newEm.close();
		}
		return true;
	}
	
	
}
